#ifndef SERVERCHECKER_H
#define SERVERCHECKER_H

#include <QNetworkReply>
#include <QObject>

#include "common.h"

class QNetworkAccessManager;
class PingInterface;
class Server;
class QTimer;

class ServerChecker : public QObject
{
    Q_OBJECT

public:
    explicit ServerChecker( int checkInterval, QObject *parent = NULL );
    
    /**
     * @brief Set server list.
     * @param servers The server list.
     */
    void setServers( ServerList *servers );

    /**
     * @brief Executes a check of all servers and
     *        starts the repeated checks.
     */
    void start();

signals:

public slots:

private:
    int mCheckInterval;
    int mCurrentServerInCheck;
    QNetworkAccessManager *mNetworkManager;
    PingInterface *mPing;
    ServerList *mServers;
    QTimer *mTimer;

    /**
     * @brief Finish check of servers.
     */
    void finishCheck();

    /**
     * @brief Ping the next server.
     */
    void pingNextServer();

    /**
     * @brief Show current data of servers in model.
     */
    void refreshModel();

private slots:
    /**
     * @brief Host is unavailable.
     */
    void hostUnavailable();

    /**
     * @brief Network error.
     * @param code The error code.
     */
    void networkError( QNetworkReply::NetworkError code );

    /**
     * @brief Handly network replys.
     * @param reply The network reply to handle.
     */
    void replyFinished( QNetworkReply *reply );

    /**
     * @brief Result of a ping.
     * @param int The delay in milliseconds (ms).
     */
    void pingResult( int delay );

    /**
     * @brief Start check of servers. Assumes that
     *        the server list is not null.
     */
    void startCheck();
};

#endif // SERVERCHECKER_H
